package com.gph.saviorframework.fastdfs.monitor.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.fastdfs.StorePath;
import com.gph.saviorframework.fastdfs.monitor.dao.FdfsFileDao;
import com.gph.saviorframework.fastdfs.monitor.model.FdfsFile;
import com.gph.saviorframework.fastdfs.monitor.service.DfsFileService;
import com.gph.saviorframework.fastdfs.service.IStorageClientService;
import com.gph.saviorframework.fastdfs.service.impl.ByteArrayFdfsFileInputStreamHandler;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;

public class DfsFileServiceImpl implements DfsFileService {

    @Autowired
    private IStorageClientService storageClientService;

    @Autowired
    private FdfsFileDao fdfsFileDao;

    /**
     * 上传文件
     *
     * @param groupName
     * @param bs
     * @param fileLength
     * @param fileName
     * @return
     */
    public String uploadFile(String groupName, byte[] bs, long fileLength, String fileName,String ip) {
        StorePath storePath = storageClientService.uploadFile(groupName,new ByteArrayInputStream(bs),
                fileLength,FilenameUtils.getExtension(fileName));
        try {
            if(storePath!=null){

                FdfsFile fdfsFile = new FdfsFile();
                fdfsFile.setId(UUID.randomUUID().toString());
                fdfsFile.setCreated(new Date());
                fdfsFile.setFileId(storePath.getGroup()+"/"+storePath.getPath());
                fdfsFile.setFileSize(fileLength);
                fdfsFile.setFileName(fileName);
                fdfsFile.setType(FilenameUtils.getExtension(fileName));
                fdfsFile.setIp(ip);
                fdfsFileDao.save(fdfsFile);

                return storePath.getGroup()+"/"+storePath.getPath();
            }
        }
        catch (Exception ex){
            deleteFile(storePath.getGroup()+"/"+storePath.getPath());
        }
        return null;
    }

    /**
     * 上传文件
     *
     * @param groupName
     * @param bs
     * @param fileName
     * @param ip
     * @return
     */
    public String uploadFile(String groupName, byte[] bs, String fileName, String ip) {
        return uploadFile(groupName,bs,bs.length,fileName,ip);
    }

    /**
     * 上传文件，groupname由fastdfs自行选择
     *
     * @param bs
     * @param fileLength
     * @param fileName
     * @return
     */
    public String uploadFile(byte[] bs, long fileLength, String fileName,String ip) {
        return uploadFile(null,bs,fileLength,fileName,ip);
    }

    /**
     * 上传文件
     *
     * @param bs
     * @param fileName
     * @param ip
     * @return
     */
    public String uploadFile(byte[] bs, String fileName, String ip) {
        return uploadFile(bs,bs.length,fileName,ip);
    }

    /**
     * 更具fdfs的相对路径删除文件
     *
     * @param path 格式为group1/M00/00/00/wKgBbVdxReKAGcvBAAE4E08WEfw817.jpg
     */
    public void deleteFile(String path) {
        String group1 = path.substring(0, path.indexOf("/"));
        String p = path.substring(path.indexOf("/")+1,path.length());
        deleteFile(group1,p);
    }

    /**
     * 根据groupname和filepath删除文件
     *
     * @param groupName 例如group1
     * @param filePath  格式为M00/00/00/wKgBbVdxReKAGcvBAAE4E08WEfw817.jpg
     */
    public void deleteFile(String groupName, String filePath) {
        storageClientService.deleteFile(groupName,filePath);
        fdfsFileDao.deleteByFileId(groupName+"/"+filePath);
    }

    /**
     * 下载文件
     *
     * @param path
     */
    public byte[] downloadFile(String path) {
        String group1 = path.substring(0, path.indexOf("/"));
        String p = path.substring(path.indexOf("/")+1,path.length());
        return downloadFile(group1,p);
    }

    /**
     * 下载文件
     *
     * @param groupName 例如group1
     * @param path      格式为M00/00/00/wKgBbVdxReKAGcvBAAE4E08WEfw817.jpg
     * @return
     */
    public byte[] downloadFile(String groupName, String path) {
        byte[] bs = storageClientService.downloadFile(groupName,path,new ByteArrayFdfsFileInputStreamHandler());
        return bs;
    }

    /**
     * 获取所有上传文件
     *
     * @param sort
     * @param dir
     * @return
     */
    public List<Map<String,Object>> find(String sort, String dir) {
        return fdfsFileDao.find(sort,dir);
    }

    /**
     * 获取所有上传文件,分页获取
     *
     * @param start
     * @param limit
     * @param sort
     * @param dir
     * @return
     */
    public Map<String, Object> find(int start, int limit, String sort, String dir) {
        Map<String,Object> map = new HashMap<String, Object>();

        PageHelper.startPage(start, limit);
        List<Map<String,Object>>list = find(sort, dir);
        PageInfo pageInfo = new PageInfo(list);

        map.put(Constants.DEFAULT_RECORD_MODEL_KEY, list);
        map.put(Constants.DEFAULT_COUNT_MODEL_KEY, pageInfo.getPages());
        map.put(Constants.DEFAULT_RECORDS_MODEL_KEY,pageInfo.getTotal());
        map.put(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);

        return map;
    }
}
